﻿using IdentityServer4.SqlSugarCore.DbContexts;
using IdentityServer4.SqlSugarCore.Entities;
using IdentityServer4.SqlSugarCore.Interfaces;
using Microsoft.Extensions.Logging;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace IdentityServer4.SqlSugarCore.TokenCleanups
{
    public class SugarPersistedGrants : IPersistedGrants
    {
        private readonly ILogger<SugarPersistedGrants> _logger;
        private readonly SugarStoreOptions _configurationStoreOptions;
        private readonly ISqlSugarClient _db;

        public SugarPersistedGrants(ILogger<SugarPersistedGrants> logger, SugarStoreOptions configurationStoreOptions, DbContext dbContext)
        {
            _logger = logger;
            _configurationStoreOptions = configurationStoreOptions;
            _db = dbContext.Db;
        }

        /// <summary>
        /// 移除指定的时间过期授权信息
        /// </summary>
        /// <param name="dt">Utc时间</param>
        /// <returns></returns>
        public async Task RemoveExpireToken(DateTime dt)
        {
            await _db.Deleteable<PersistedGrant>().Where(x => x.Expiration > dt).ExecuteCommandAsync();
        }
    }
}
